﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;

using Engine.Result;

using Scheduler;

using EventArgs = Scheduler.EventArgs;

namespace Engine.Middlewares
{
    public class CommUnitMiddleware : Middleware
    {
        private Queue<(int, int)> _listValue;
        private BlockingCollection<Object> _chipsDUTs = new BlockingCollection<Object>();

        private int _count = 0;

        public CommUnitMiddleware()
        {
            var str = System.IO.File.ReadAllLines(Environment.CurrentDirectory + @"\Middlewares\PositionTest.txt");
            _listValue = new Queue<(int, int)>();

            foreach (var item in str)
            {
                var tmp = item.Split(',');
                var x = int.Parse(tmp[0]);
                var y = int.Parse(tmp[1]);
                _listValue.Enqueue((x, y));
            }
        }
        public override bool Filter(Context ctx, EventArgs args)
        {
               return true; // args.Id == EventId.Init || args.Id == EventId.InLotBegin || args.Id == EventId.Cleanup || args.Id == EventId.PassBegin || args.Id == EventId.PassEnd;
        }

        public ChipResult cr;
        protected override void DoHandleEvent(Context ctx, EventArgs args)
        {
            return;

            if (!(ctx is WaferTesterContext context))
                return;

            if (args.Id == EventId.Init && (int)args.UserData[0] == 0)
            {
                // context.TestResultPipe.AddToBuffer(0, context.Result.NextPass());


            }
            if (args.Id == EventId.PassBegin && (int)args.UserData[0] == 0)
            {

                var rel = context.Result.NextPass();
                if (rel==null)
                {
                    //context.UserCancelled = true;
                    return;
                    

                }

                while (context.TestResultPipe.Count((int)args.UserData[0])  >0)
                {

                    Console.WriteLine($"缓冲区满{(int)args.UserData[0]}");
                    
                    Thread.Sleep(0);
                }
                //context.TestResultPipe.AddToBuffer(0, rel);


            }

            if (args.Id == EventId.PassEnd)
            {

            }



        }

    }
}